﻿<%@ WebHandler Language="C#" Class="DownloadNotics" %>

using System;
using System.Web;
using System.Xml;

public class DownloadNotics : IHttpHandler
{
    string filepath = Maticsoft.Common.ConfigHelper.GetConfigString("Noticfile");
    string filetype = Maticsoft.Common.ConfigHelper.GetConfigString("GetNoticType");
    public void ProcessRequest(HttpContext context)
    {
        GetFileText(context);
    }

    /// <summary>
    /// 获取文件
    /// </summary>
    /// <param name="context"></param>
    public void GetFileText(HttpContext context)
    {
        string id = context.Request["id"];//mac地址
        if (string.IsNullOrEmpty(id))
            return;
        var filelist = BarberShop.BLL.UpdateFilesBLL.GetUpdateFilesByStoreId(int.Parse(id));
        //组装xml
        
        #region 组装xml
        XmlDocument xmldoc = new XmlDocument();
        XmlNode Xnode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8","");
        xmldoc.AppendChild(Xnode);
        XmlElement RootElm = xmldoc.CreateElement("Root");
        xmldoc.AppendChild(RootElm);

        XmlElement DatetimeElm = xmldoc.CreateElement("datetime");
        DatetimeElm.InnerText = DateTime.Now.ToString("yyyyMMddhhssmm");
        RootElm.AppendChild(DatetimeElm);
        
        XmlElement DataInfoElm = xmldoc.CreateElement("DATAINFO");
        RootElm.AppendChild(DataInfoElm);
        foreach (var item in filelist)
        {
            XmlElement SubElm = xmldoc.CreateElement("file");
            XmlElement nameElm = xmldoc.CreateElement("name");
            nameElm.InnerText=item.FileName;
            XmlElement pathElm = xmldoc.CreateElement("path");
            pathElm.InnerText = item.FilePath;
            XmlElement linkElm = xmldoc.CreateElement("link");
            linkElm.InnerText = item.LinkPath;
            SubElm.AppendChild(nameElm);
            SubElm.AppendChild(pathElm);
            SubElm.AppendChild(linkElm);
            DataInfoElm.AppendChild(SubElm);
        }
        #endregion
        
        context.Response.ClearContent();
        context.Response.ClearHeaders();
        context.Response.ContentType = "text/xml";
        context.Response.AppendHeader("content-disposition", "attachment;filename=Update.xml;");
        xmldoc.Save(context.Response.OutputStream);


    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
